///////////////////////////////////////////////////////////////////////////////
// Panel (a)
///////////////////////////////////////////////////////////////////////////////

*******************************************************************************
* Specify city and years; load data
*******************************************************************************

local city = "fort_worth"

local y1 2009
local y2 2014
local y3 2020

use dataset_`city'_cross, clear
gen totalvotesed = totalvotes - totalvotesnoned
gen ltotalvotesed = log(totalvotesed)

*******************************************************************************
* Rescale explanatory variables
*******************************************************************************

foreach x in pctnhblack distancem {
	su `x'
	replace `x' = (`x'- r(min)) / (r(max) - r(min))
}
replace distancem = -distancem + 1

*******************************************************************************
* Time interactions
*******************************************************************************

gen pctnhblackXy2 = pctnhblack*year_2
gen pctnhblackX2020 = pctnhblack*year_3
gen distanceXy2 = distancem*year_2
gen distanceX2020 = distancem*year_3

label variable pctnhblackXy2 "Share Black X 2014"
label variable pctnhblackX2020 "Share Black X 2020"
label variable distanceXy2 "Proximity to Shooting X 2014"
label variable distanceX2020 "Proximity to Shooting X 2020"

*******************************************************************************
* Do the regressions
*******************************************************************************

eststo clear

eststo: reg pctyesed pctnhblack pctnhblackXy2 pctnhblackX2020 ///
	distancem distanceXy2 distanceX2020 year_2 year_3 x_c y_c ///
	[aw=totalvotesed], robust 
estadd local yfe = "X"
estadd local wtvote = "X"
estadd local clvote = ""
estadd local latlon = "X"
estadd local myse = "Robust"
	
foreach l in 1 2 5 {
	eststo: acreg pctyesed pctnhblack pctnhblackXy2 pctnhblackX2020 ///
		distancem distanceXy2 distanceX2020 ///
		x_c y_c year_2 year_3 ltotalvotesed, ///
		spatial latitude(y_c) longitude(x_c) distcutoff(`l') ///
		hac id(precinctyear) time(year) lagcutoff(0)
	estadd local slag = `l'
	estadd local yfe = "X"
	estadd local wtvote = ""
	estadd local clvote = "X"
	estadd local latlon = "X"
	estadd local myse = "HAC"
}

*******************************************************************************
* Output table
*******************************************************************************

#delimit;

esttab
	using "_output/table A24 a.tex"
	,
		replace
		booktabs
		label
		nomtitles
		collabels(none)
		drop(*_c ltotal* year_2 year_3 _cons)
		cells(b(fmt(3) star) se(par))
		width(\linewidth)
		stats(yfe wtvote clvote latlon myse slag N, 
			label(
				"Year Fixed Effects"
				"Weight by Total Votes Cast"
				"Control for Log(Total Votes Cast)"
				"Control for Latitude \& Longitude"
				"Standard Errors"
				"Distance Cutoff (Miles)"
				"Number of Precincts" 
			)
			fmt(%20.0f %20.0f))
		compress
		alignment(l)	
		gap
		;
			
#delimit cr

///////////////////////////////////////////////////////////////////////////////
// Panel (b)
///////////////////////////////////////////////////////////////////////////////

*******************************************************************************
* Specify city and years; load data
*******************************************************************************

local city = "fort_worth"

local y1 2009
local y2 2014
local y3 2020

use dataset_`city'_cross, clear
gen ltotalvotesnoned = log(totalvotesnoned)

*******************************************************************************
* Rescale explanatory variables
*******************************************************************************

foreach x in pctnhblack distancem {
	su `x'
	replace `x' = (`x'- r(min)) / (r(max) - r(min))
}
replace distancem = -distancem + 1

*******************************************************************************
* Time interactions
*******************************************************************************

gen pctnhblackXy2 = pctnhblack*year_2
gen pctnhblackX2020 = pctnhblack*year_3
gen distanceXy2 = distancem*year_2
gen distanceX2020 = distancem*year_3

label variable pctnhblackXy2 "Share Black X 2014"
label variable pctnhblackX2020 "Share Black X 2020"
label variable distanceXy2 "Proximity to Shooting X 2014"
label variable distanceX2020 "Proximity to Shooting X 2020"

*******************************************************************************
* Do the regressions
*******************************************************************************

eststo clear

eststo: reg pctyesnoned pctnhblack pctnhblackXy2 pctnhblackX2020 ///
	distancem distanceXy2 distanceX2020 year_2 year_3 x_c y_c ///
	[aw=totalvotesnoned], robust 
estadd local yfe = "X"
estadd local wtvote = "X"
estadd local clvote = ""
estadd local latlon = "X"
estadd local myse = "Robust"
	
foreach l in 1 2 5 {
	eststo: acreg pctyesnoned pctnhblack pctnhblackXy2 pctnhblackX2020 ///
		distancem distanceXy2 distanceX2020 ///
		x_c y_c year_2 year_3 ltotalvotesnoned, ///
		spatial latitude(y_c) longitude(x_c) distcutoff(`l') ///
		hac id(precinctyear) time(year) lagcutoff(0)
	estadd local slag = `l'
	estadd local yfe = "X"
	estadd local wtvote = ""
	estadd local clvote = "X"
	estadd local latlon = "X"
	estadd local myse = "HAC"
}

*******************************************************************************
* Output table
*******************************************************************************

#delimit;

esttab
	using "_output/table A24 b.tex"
	,
		replace
		booktabs
		label
		nomtitles
		collabels(none)
		drop(*_c ltotal* year_2 year_3 _cons)
		cells(b(fmt(3) star) se(par))
		width(\linewidth)
		stats(yfe wtvote clvote latlon myse slag N, 
			label(
				"Year Fixed Effects"
				"Weight by Total Votes Cast"
				"Control for Log(Total Votes Cast)"
				"Control for Latitude \& Longitude"
				"Standard Errors"
				"Distance Cutoff (Miles)"
				"Number of Precincts" 
			)
			fmt(%20.0f %20.0f))
		compress
		alignment(l)	
		gap
		;
			
#delimit cr
	
///////////////////////////////////////////////////////////////////////////////
// End
///////////////////////////////////////////////////////////////////////////////
